package com.learn.prototype.deep;

import lombok.Getter;
import lombok.Setter;

import java.io.*;

/**
 * <p>项目名称: design-pattern </p>
 *
 * <p>文件名称: DeepMonkey </p>
 *
 * <p>描述: 孙悟空 </p>
 */
@Getter
@Setter
public class DeepMonkey implements Cloneable,Serializable {
    private static final long serialVersionUID = 982851439286019395L;

    private String name;

    private int age;

    private DeepClub club;

    @Override
    public DeepMonkey clone() throws CloneNotSupportedException {
        return (DeepMonkey)super.clone();
    }

    public DeepMonkey deepClone() throws IOException, ClassNotFoundException{
        //将对象写到流里
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(bos);
        oos.writeObject(this);
        //从流里读回来
        ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
        ObjectInputStream ois = new ObjectInputStream(bis);
        return (DeepMonkey) ois.readObject();
    }
}
